package com.han.hotelplat.cms.controller.datacenter.employee;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.service.datacenter.employee.EmployeeDataService;
/**
 * 
	 ***************************************************************
	 * <p>
	 * @CLASS				:	CMSDataEmController
	 * @DESCRIPTION	:	
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @DATE					:	2018年10月29日 下午5:35:56            
	 * </p>
	 * 
	 * <p>
	 * @MODIFY	LOG	:		
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @MODIFY DATE	:	
	 * </p>    
	 ****************************************************************
 */
@Controller
@RequestMapping("/cms/datacenter/employee")
public class CMSDataEmController {
	@Autowired
	private EmployeeDataService employeeDataService;
	/**
	 * 获取帮工数据
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeByState", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeByState(){
		Map<String, Integer> totalData = employeeDataService.getEmployeeGroupByState();
		return new ResponseWrapTo(0, "数据获取成功", totalData).toJson();
	}
	
	/**
	 * 获取帮工数据按年龄分组
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeGroupByAge", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeGroupByAge(){
		Map<String, Object> totalData = employeeDataService.getEmployeeGroupByAge();
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Object> en : totalData.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("age", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}
		return new ResponseWrapTo(0, "数据获取成功", returnData).toJson();
	}
	
	/**
	 * 获取帮工数据按区域分组
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeGroupByArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeGroupByArea(Integer groupArea){
		List<Map<String, Object>> totalData = employeeDataService.getEmployeeGroupByArea(groupArea);
		return new ResponseWrapTo(0, "数据获取成功", totalData).toJson();
	}
	
	/**
	 * 获取帮工数据按工作年限分组
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeGroupByWorkYear", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeGroupByWorkYear(){
		Map<String, Object> totalData = employeeDataService.getEmployeeGroupByWorkYear();
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Object> en : totalData.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("workYear", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}
		return new ResponseWrapTo(0, "数据获取成功", returnData).toJson();
	}
	
	/**
	 * 获取帮工数据按性别分组
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeGroupBySex", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeGroupBySex(){
		Map<String, Object> totalData = employeeDataService.getEmployeeGroupBySex();
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Object> en : totalData.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("sex", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}
		return new ResponseWrapTo(0, "数据获取成功", returnData).toJson();
	}
	
	/**
	 * 获取帮工数据按工作状态分组
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getEmployeeGroupByWorkState", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getEmployeeGroupByWorkState(String startTime, String endTime){
		Map<String, Object> totalData = employeeDataService.getEmployeeGroupByWorkState(startTime, endTime);
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Object> en : totalData.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("workState", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}
		return new ResponseWrapTo(0, "数据获取成功", returnData).toJson();
	}
	
	/**
	 * 获取帮工日新增数据
	 * 
		 * @description	
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAddEmployeeGroupByDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getAddEmployeeGroupByDate(String startTime, String endTime){
		if(StringUtils.isBlank(startTime)){
			return new ResponseWrapTo(-1, "请输入起始日期", null).toJson();
		}
		if(StringUtils.isBlank(endTime)){
			return new ResponseWrapTo(-1, "请输入结束日期", null).toJson();
		}
		Map<String, Integer> totalData = employeeDataService.getAddEmployeeGroupByDate(startTime, endTime);
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Integer> en : totalData.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("date", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}
		returnData.clear();
		List<Date> allDate = DateUtil.getBetweenDates(DateUtil.parseYMD(startTime), DateUtil.parseYMD(endTime));
		for(Date date : allDate){
			Integer count = (Integer) totalData.get(DateUtil.formatYMD(date));
			if(count == null){
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", 0);
				returnData.add(sub);
			}else{
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", count);
				returnData.add(sub);
			}
		}
		return new ResponseWrapTo(0, "数据获取成功", returnData).toJson();
	}
	
}
